// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from '@/App'
import router from '@/router'
import store from '@/store'
import globalConstant from '@/constant/vue_global'
import utils from '@/lib/utils'
import strategies from '@/lib/strategies'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import VueProgressiveImage from '@/lib/vue-progressive-image/'
Vue.use(VueAwesomeSwiper)
Vue.use(VueProgressiveImage)

for (let k in globalConstant) {
  if (globalConstant.hasOwnProperty(k)) {
    Vue.prototype[k] = globalConstant[k]
  }
}

Vue.config.productionTip = false

function startApp(){
  new Vue({
    el: '#app',
    router,
    store,
    template: '<App/>',
    components: { App }
  })
}

utils.ObjectAssignPolyfill()

//路由检查
router.beforeEach((to, from, next) => {
  let strategiesCollection = strategies.route,
      routeInfo = {to, from},
      context = function(key, params){
        let fn = strategiesCollection[key]
        if(!fn || typeof fn !== 'function') return null
        return fn(params)
      }
  to.matched.forEach(record => {
    let meta = record.meta

    for(let key in meta){
      let params = {
        value: meta[key],
        route: routeInfo
      }
      let result = context(key, params)
      if(result === null){
        continue
      } else if(result === false) {
        throw new Error('should not push this route')
      }
    }

  })
  next()//检查通过后必须执行next
})

startApp()
